/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.persistence.jdbc;

import org.merak.core.model.SettingException;
import org.merak.core.persistence.jdbc.settings.ConnectionDetail;

public abstract class DatabaseSystem {

	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	public static DatabaseSystem getInstance(String name)
		throws SettingException, IllegalAccessException
	{
		try {
			// Check empty string
			if (name == null || name.isEmpty()) throw new IllegalArgumentException(
				"No name provided for Database System. Impossible to find a plugin."
			);

			// Prepare system name with a first capital letter
			name = new StringBuffer(name.length())
		          .append(Character.toTitleCase(name.charAt(0)))
		          .append(name.substring(1))
		          .toString();

			// Search for class
			return (DatabaseSystem) Class.forName(
				DatabaseSystem.class.getPackage().getName() + ".systems." + name
        	).newInstance();
    	}
    	catch (ClassNotFoundException e) { throw new SettingException(
   			"No plugin for Database System: " + name
    	);}
    	catch (InstantiationException e) { throw new SettingException(
       		"Cannot instantiate the plugin for Database System: " + name
       	);}
    	catch (NoClassDefFoundError e) { throw new SettingException(
       		"Mistyped plugin name for Database System: " + e.getMessage()
       	);}
    	catch (IllegalAccessException e) {
    		throw new RuntimeException(e);
    	}

	}
	//****************************************************************************
	public abstract String getStandardDriver();

	//****************************************************************************
	public abstract Integer getStandardPort();

	//****************************************************************************
	public abstract String formatUrl(ConnectionDetail settings) throws Exception;

	//****************************************************************************
	@Override
	public String toString() {
		return this.getClass().getSimpleName();
	}
	//****************************************************************************

}

